package com.wg.dome.attribute;

import com.wg.common.lang.HashTable;

import java.util.HashMap;
import java.util.Map;

public class Attribute {
    private final HashMap<AttributeType, Integer> data = new HashMap<>();

    public void set(AttributeType attributeType, int value) {
        if (data.containsKey(attributeType)) {
            data.put(attributeType, data.get(attributeType) + value);
        } else {
            data.put(attributeType, value);
        }
    }

    public int get(AttributeType attributeType) {
        return data.get(attributeType);
    }

    public void addAll(Attribute a2) {
        for (Map.Entry<AttributeType, Integer> entry : a2.data.entrySet()) {
            if (data.containsKey(entry.getKey())) {
                data.put(entry.getKey(), data.get(entry.getKey()) + entry.getValue());
            } else {
                data.put(entry.getKey(), entry.getValue());
            }
        }
    }

    public HashTable toHashTable() {
        HashTable hashTable = new HashTable(data.size());
        for (Map.Entry<AttributeType, Integer> entry : data.entrySet()) {
            hashTable.put((byte) entry.getKey().ordinal(), entry.getValue());
        }

        return hashTable;
    }
}
